//
//  AyHomeViewController.m
//  weibo
//
//  Created by alvis mac on 8/1/15.
//  Copyright (c) 2015 alvis. All rights reserved.
//

#import "AyStatusSendController.h"
#import "UIBarButtonItemExtend.h"
#import "AyDefine.h"
#import "AyTitleButton.h"
#import "UIImageExtend.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "MJExtension.h"
#import "AyStatusFrame.h"
#import "AyAccountTool.h"
#import "AyStatusCell.h"
#import "MJRefresh.h"
#import "AyUserInfoParam.h"
#import "AyUserInfoResult.h"
#import "AyUserTool.h"
#import "AyHomeStatusesParame.h"
#import "AyHomeStatusesResult.h"
#import "AyStatusTool.h"

@interface AyStatusSendController ()<MJRefreshBaseViewDelegate>
@property (nonatomic, strong)  NSMutableArray * statusFrames;
@property (nonatomic, weak)  MJRefreshHeaderView * header;
@property (nonatomic, weak)  MJRefreshFooterView * footer;
@end


@implementation AyStatusSendController

- (NSArray *)statusFrames{
    if (_statusFrames == nil) {
        _statusFrames=[NSMutableArray array];
    }
    return _statusFrames;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupRefreshView];
    [self setupTableView];
}


- (void)setupTableView
{
     self.title = @"全部微博";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = AyColor(226, 226, 226);
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, AyStatusTableBorder, 0);
}



- (void)setupRefreshView
{
    MJRefreshHeaderView *header=[MJRefreshHeaderView header];
    header.scrollView=self.tableView;
    header.delegate=self;
    [header beginRefreshing];
    self.header=header;
    
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = self.tableView;
    footer.delegate = self;
    self.footer = footer;
}


-(void)dealloc{
    [self.header free];
    [self.footer free];
}

- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView{
    if ([refreshView isKindOfClass:[MJRefreshFooterView class]]) {
        [self loadMoreData];
    } else { // 下拉刷新
        [self loadNewData];
    }
}

- (void) loadMoreData{
    
    AyHomeStatusesParame *param=[AyHomeStatusesParame param];
    if (self.statusFrames.count) {
        AyStatusFrame *statusFrame=[self.statusFrames lastObject];
        param.max_id=@([statusFrame.status.idstr longLongValue]-1);
    }
    
    [AyStatusTool userStatusesWithParam:param success:^(AyHomeStatusesResult *result) {
        NSMutableArray *statusFramArray = [NSMutableArray array];
        for(AyStatus *status in result.statuses){
            AyStatusFrame *sframe=[[AyStatusFrame alloc] init];
            sframe.status=status;
            [statusFramArray addObject:sframe];
        }
        [self.statusFrames addObjectsFromArray:statusFramArray];
        [self.tableView reloadData];
        [self.footer endRefreshing];
    } failure:^(NSError *error) {
        [self.footer endRefreshing];
    }];
}

- (void) loadNewData{
    self.tabBarItem.badgeValue=nil;
    
    AyHomeStatusesParame *param=[AyHomeStatusesParame param];
    if (self.statusFrames.count) {
        AyStatusFrame *statusFrame=[self.statusFrames firstObject];
        param.since_id=@([statusFrame.status.idstr longLongValue]);
    }
    
    [AyStatusTool userStatusesWithParam:param success:^(AyHomeStatusesResult *result) {
        NSMutableArray *statusFramArray=[NSMutableArray array];
        for(AyStatus *status in result.statuses){
            AyStatusFrame *sframe=[[AyStatusFrame alloc] init];
            sframe.status=status;
            [statusFramArray addObject:sframe];
        }
        
        NSMutableArray *tempArray = [NSMutableArray array];
        [tempArray addObjectsFromArray:statusFramArray];
        [tempArray addObjectsFromArray:self.statusFrames];
        self.statusFrames = tempArray;
        [self.tableView reloadData];
        [self.header endRefreshing];

    } failure:^(NSError *error) {
        [self.header endRefreshing];
    }];
    
}




#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.statusFrames.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AyStatusCell *cell=[AyStatusCell cellWithTableView:tableView];
    cell.statusFrame=self.statusFrames[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AyStatusFrame *statusFrame = self.statusFrames[indexPath.row];
    return statusFrame.cellHeight;
}



@end
